#!/bin/bash

ARGS=("$@")

if [ ${#ARGS[@]} -lt 11 ]
then
	echo "Error: incorrect number of arguments."
	echo "Usage: train-and-output-latex.sh run_latex config_file output_dir gloss_file gloss_file_format ds_file ds_tree_format ds_file_format ps_file ps_tree_format ps_file_format [pb_file pb_tree_format pb_file_format]"
	exit
fi

script_dir=$(dirname $0)
dist=$script_dir/../bin
pkg=edu.washington.ling.syntric.proc
jar_dir=$script_dir/../jars
latex_dir=$script_dir/../latex

compile_latex=${ARGS[0]}
config_file=${ARGS[1]}
output_dir=${ARGS[2]}
gloss_file=${ARGS[3]}
gloss_file_format=${ARGS[4]}
ds_file=${ARGS[5]}
ds_tree_format=${ARGS[6]}
ds_file_format=${ARGS[7]}
ps_file=${ARGS[8]}
ps_tree_format=${ARGS[9]}
ps_file_format=${ARGS[10]}
if [ ${#ARGS[@]} -eq 14 ]
then
    pb_file=${ARGS[11]}
    pb_tree_format=${ARGS[12]}
    pb_file_format=${ARGS[13]}
fi

# Set up output directory
if [ ! -d $output_dir ]; then
	if ! mkdir $output_dir ; then
		echo Error: output_dir=\'$output_dir\' was not valid
		echo Please specify a valid output directory
		exit
	fi
fi

echo %%
echo %% 00 Creating Example File From Input Tree Files 
echo %%
if [ -d $output_dir/00-examples ]; then
	rm -rf $output_dir/00-examples
fi
mkdir $output_dir/00-examples
if [ ! -z $pb_file ]
then
    java -cp $dist:$jar_dir/* $pkg.CreateUnifiedExamples --input-file $gloss_file --input-file-format $gloss_file_format --output-file $output_dir/00-examples/examples.xml --output-file-format xml --ds-file $ds_file --ds-file-format $ds_file_format --ds-tree-format $ds_tree_format --pb-file $pb_file --pb-file-format $pb_file_format --pb-tree-format $pb_tree_format --ps-file $ps_file --ps-file-format $ps_file_format --ps-tree-format $ps_tree_format 2> $output_dir/00-examples/errors.txt
else
    java -cp $dist:$jar_dir/* $pkg.CreateUnifiedExamples --input-file $gloss_file --input-file-format $gloss_file_format --output-file $output_dir/00-examples/examples.xml --output-file-format xml --ds-file $ds_file --ds-file-format $ds_file_format --ds-tree-format $ds_tree_format --ps-file $ps_file --ps-file-format $ps_file_format --ps-tree-format $ps_tree_format 2> $output_dir/00-examples/errors.txt
fi


echo 
echo %%
echo %% 10 Checking DS Format
echo %%
if [ -d $output_dir/10-check-ds ]; then
	rm -rf $output_dir/10-check-ds
fi
mkdir $output_dir/10-check-ds
java -cp $dist:$jar_dir/* $pkg.CheckDSFormat $output_dir/00-examples/examples.xml $output_dir/10-check-ds/examples.xml --tree-format $ds_tree_format 2> $output_dir/10-check-ds/errors.txt

if [ ! -z $pb_file ]
then
    echo 
    echo %%
    echo %% 11 Checking PB Format
    echo %%
    if [ -d $output_dir/11-check-pb ]; then
	rm -rf $output_dir/11-check-pb
    fi
    mkdir $output_dir/11-check-pb
    java -cp $dist:$jar_dir/* $pkg.CheckPBFormat $output_dir/10-check-ds/examples.xml $output_dir/11-check-pb/examples.xml --tree-format $pb_tree_format 2> $output_dir/11-check-pb/errors.txt
fi

echo 
echo %%
echo %% 12 Checking PS Format
echo %%
if [ -d $output_dir/12-check-ps ]; then
	rm -rf $output_dir/12-check-ps
fi
if [ ! -z $pb_file ]
then
    input_file=$output_dir/11-check-pb/examples.xml
else
    input_file=$output_dir/10-check-ds/examples.xml
fi
mkdir $output_dir/12-check-ps
java -cp $dist:$jar_dir/* $pkg.CheckPSFormat $input_file $output_dir/12-check-ps/examples.xml --tree-format $ps_tree_format 2> $output_dir/12-check-ps/errors.txt

if [ ! -z $pb_file ]
then
    echo 
    echo %%
    echo %% 20 Merging DS and PB to create DS+PB
    echo %%
    if [ -d $output_dir/20-merge-ds-pb ]; then
	rm -rf $output_dir/20-merge-ds-pb
    fi
    mkdir $output_dir/20-merge-ds-pb
    java -cp $dist:$jar_dir/* $pkg.MergeDSandPB $output_dir/12-check-ps/examples.xml $output_dir/20-merge-ds-pb/examples.xml --ds-tree-format $ds_tree_format --pb-tree-format $pb_tree_format 2> $output_dir/20-merge-ds-pb/errors.txt
fi

echo 
echo %%
echo %% 30 Applying Consistency Rules to Create DS_Const
echo %%
if [ -d $output_dir/30-apply-const-rules ]; then
	rm -rf $output_dir/30-apply-const-rules
fi
if [ ! -z $pb_file ]
then
    input_file=$output_dir/20-merge-ds-pb/examples.xml
else
    input_file=$output_dir/12-check-ps/examples.xml
fi
mkdir $output_dir/30-apply-const-rules
java -cp $dist:$jar_dir/* $pkg.ApplyConsistencyRules $input_file $output_dir/30-apply-const-rules/examples.xml --ds-tree-format $ds_tree_format 2> $output_dir/30-apply-const-rules/errors.txt

echo 
echo %%
echo %% 40 Checking Consistency of DS_Const and PS
echo %%
if [ -d $output_dir/40-check-const ]; then
	rm -rf $output_dir/40-check-const
fi
mkdir $output_dir/40-check-const
java -cp $dist:$jar_dir/* $pkg.CheckConsistency $output_dir/30-apply-const-rules/examples.xml $output_dir/40-check-const/examples.xml --ds-tree-format $ds_tree_format --ps-tree-format $ps_tree_format 2> $output_dir/40-check-const/errors.txt

echo 
echo %%
echo %% 50 Converting Processed Examples to LaTeX
echo %%
if [ -d $output_dir/50-procd-examples ]; then
	rm -rf $output_dir/50-procd-examples
fi
mkdir $output_dir/50-procd-examples
mkdir $output_dir/50-procd-examples/latex
java -cp $dist:$jar_dir/* $pkg.ConvertExamplesToLaTeX $output_dir/40-check-const/examples.xml $output_dir/50-procd-examples/latex/training.tex --include PS --include DS-Const --include DS --include PB 2> $output_dir/50-procd-examples/errors.txt

echo 
echo %%
echo %% 60 Extracting unary conversion rules
echo %%
if [ -d $output_dir/60-extract-unary ]; then
	rm -rf $output_dir/60-extract-unary
fi
mkdir $output_dir/60-extract-unary
mkdir $output_dir/60-extract-unary/latex
java -cp $dist:$jar_dir/* $pkg.ExtractUnaryRules $output_dir/40-check-const/examples.xml $output_dir/60-extract-unary/rules.txt --ds-tree-format $ds_tree_format --ps-tree-format $ps_tree_format --config $config_file --latex-output $output_dir/60-extract-unary/latex/ambig-rules.tex 2> $output_dir/60-extract-unary/errors.txt

echo 
echo %%
echo %% 61 Extracting unary pos-mapping rules
echo %%
if [ -d $output_dir/61-extract-pos-map ]; then
	rm -rf $output_dir/61-extract-pos-map
fi
mkdir $output_dir/61-extract-pos-map
mkdir $output_dir/61-extract-pos-map/latex
java -cp $dist:$jar_dir/* $pkg.ExtractUnaryRules $output_dir/40-check-const/examples.xml $output_dir/61-extract-pos-map/rules.txt --ds-tree-format $ds_tree_format --ps-tree-format $ps_tree_format --config $config_file --latex-output $output_dir/61-extract-pos-map/latex/ambig-rules.tex -p 2> $output_dir/61-extract-pos-map/errors.txt

echo 
echo %%
echo %% 70 Extracting binary conversion rules
echo %%
if [ -d $output_dir/70-extract-binary ]; then
	rm -rf $output_dir/70-extract-binary
fi
mkdir $output_dir/70-extract-binary
mkdir $output_dir/70-extract-binary/latex
java -cp $dist:$jar_dir/* $pkg.ExtractBinaryRules $output_dir/40-check-const/examples.xml $output_dir/70-extract-binary/rules.txt --ds-tree-format $ds_tree_format --ps-tree-format $ps_tree_format --config $config_file --latex-output $output_dir/70-extract-binary/latex/ambig-rules.tex 2> $output_dir/70-extract-binary/errors.txt

if $compile_latex; then
	$latex_dir/scripts/compile-latex-under-dir.sh $output_dir
fi
